package cc.angis.xiugallery.provider;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

public class GalleryImageProvider implements ISourceProvider{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8070527911565519418L;

	private String bucketId;
	
	private Context context;
	
	private List<Long> imageIds;
	
	public GalleryImageProvider(Context context, String bucketId){
		this.bucketId = bucketId;
		this.context = context;
		imageIds =new ArrayList<Long>();
	}

	@Override
	public Object getItem(int num) {
		return imageIds.get(num);
	}

	@Override
	public int getType() {
		return 0;
	}

	@Override
	public int getSize() {
		return imageIds.size();
	}

	@Override
	public void initSource() {
		imageIds.clear();
		String[] projection = new String[]{
	            MediaStore.Images.Media._ID,
	    };

	    // Get the base URI for the People table in the Contacts content provider.
	    Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

	    // Make the query.
	    Cursor cur = context.getContentResolver().query(images,
	            projection, // Which columns to return
	            MediaStore.Images.Media.BUCKET_ID + "=?", // Which rows to return (all rows)
	            new String[]{bucketId},       // Selection arguments (none)
	            MediaStore.Images.Media.BUCKET_ID         // Ordering
	            );

	    Log.i("ListingImages"," query count="+cur.getCount());

	    if (cur.moveToFirst()) {
	    	long imageId;
	        int imageIdColumn = cur.getColumnIndex(
	            MediaStore.Images.Media._ID);

	        do {
	            // Get the field values
	        	imageId = cur.getLong(imageIdColumn);

	        	imageIds.add(imageId);
	            // Do something with the values.
//	            Log.i("ListingImages", " bucket=" + bucket 
//	                   + "  date_taken=" + date);
	        } while (cur.moveToNext());

	    }
	}

	@Override
	public String getItemText(int num) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Object getImageItem(int num) {
		return getItem(num);
	}

}
